package com.sinacom.ibuilding.action;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.sinacom.ibuilding.beans.Member;
import com.sinacom.ibuilding.beans.Message;
import com.sinacom.ibuilding.beans.Sms;
import com.sinacom.ibuilding.service.IdCreateService;
import com.sinacom.ibuilding.service.MemberService;
import com.sinacom.ibuilding.service.MessageService;
import com.sinacom.ibuilding.util.Constants;




public class AcceptMsgAction {
	MemberService memberService = new MemberService();
	MessageService messageService = new MessageService();
	private IdCreateService idService = new IdCreateService();
	private List msgList;
	
	public String index() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest(); 
		request.setCharacterEncoding("GBK");
		String fromMobile = request.getParameter("FromMobile");
		String content = request.getParameter("Content");
		String numTag = request.getParameter("NumTag");
		if(!StringUtils.isEmpty(fromMobile)){
			if(!StringUtils.isEmpty(content)){
				content = URLDecoder.decode(content, "GBK");
			}
			Member sendMember = memberService.getMemberMobile(fromMobile);
			if(sendMember != null){
				List smsList = messageService.getSmsList(fromMobile, numTag);
				if(smsList != null && smsList.size() > 0){
					for (int i = 0; i < smsList.size(); i++) {
						Sms sms = (Sms) smsList.get(i);
						Member receiveMember = memberService.getMemberMobile(sms.getSendMobile());
						if(receiveMember != null){
							Message message = new Message();
							Integer id = idService.generateId(Constants.TABLE_MESSAGE);
							message.setId(String.valueOf(id));
							Date now = new Date();
							message.setCreateDate(now);
							message.setModifyDate(now);
							message.setIsRead(new Boolean(false));
							message.setSendStatus(new Boolean(true));
							message.setType(new Integer(Constants.MSG_TYPE_SHORT));
							message.setSmember(sendMember.getId());
							message.setRmember(receiveMember.getId()+"#P#"+receiveMember.getUsername());
//							message.setRmember("("+receiveMember.getId()+",网站,"+receiveMember.getUsername()+")");
							message.setContent(content);
							message.setMember(receiveMember.getId());
							message.setBoxType(new Integer(Constants.MSG_BOX_RECEIVE));
							messageService.saveMessage(message);
						}
					}
					
				}
			}
		}
		
		
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=UTF-8");
        PrintWriter pw = null;
        pw = response.getWriter();
        pw.write("Receive");
		return null;
	}
	
	/*public String deletesms() throws IOException{
		messageService.deleteSms();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=UTF-8");
        PrintWriter pw = null;
        pw = response.getWriter();
        pw.write("删除成功");
		return null;
	}*/
	public String getmsg(){
		msgList = messageService.getMessageList();
		return "msgList";
	}
	
	
	public List getMsgList() {
		return msgList;
	}

	public void setMsgList(List msgList) {
		this.msgList = msgList;
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		String str = URLEncoder.encode("这是一个测试","GBK");
		System.out.println(str);
		System.out.println(URLDecoder.decode(str, "GBK"));
	}
}
